<?php

class Topcmmchat_Component_Controller_Admincp_Index extends Phpfox_Component
{
    public function process()
    {
        $aSettings = Phpfox::getService('topcmmchat') -> getSettings();
        $serverhost = $aSettings['fc_server_host'];
        $serverhost = str_replace('http://','',$serverhost);
        $serverhost = str_replace('/','',$serverhost);

        //$c_loc = 1;
        //anthony add
		$server_host = $serverhost;
    	/*$c_check = Phpfox::getService('topcmmchat') -> validate_client('http://' . $_SERVER['HTTP_HOST'] . ':35555/');
        if ($c_check == 0){
            $c_loc = 0;
        } */
		$c_loc = 1;
		if($aSettings['fc_server_host'] == $_SERVER['HTTP_HOST']){
			$c_check = Phpfox::getService('topcmmchat') -> validate_client('http://' . $_SERVER['HTTP_HOST'] . ':35555/');
	        if ($c_check == 0){
	            $c_loc = 0;
	        }
		}
        $aSettings['fc_extendserver'] = $this->request()->get('fc_extendserver');
        if($aSettings['fc_extendserver'] !== ''){
	        if ($aSettings['fc_extendserver'] == '0'){
	        	if ($c_loc == 1){
					if ($this->request()->get('fc_server_host') != ''){
	                    $aSettings['fc_server_host'] = $this->request()->get('fc_server_host');
	                }else{
	                    $aSettings['fc_server_host'] = $_SERVER['SERVER_NAME'];
	                }
					if ($this->request()->get('fc_server_port') != ''){
	                    $aSettings['fc_server_port'] = $this->request()->get('fc_server_port');
	                }else{
	                    $aSettings['fc_server_port'] = '51127';
	                }
					if ($this->request()->get('fc_http_port') != ''){
	                    $aSettings['fc_http_port'] = $this->request()->get('fc_http_port');
	                }else{
	                    $aSettings['fc_http_port'] = '35555';
	                }
	                $aSettings['fc_client_loc'] = $this->request()->get('fc_client_loc');
	                $server_host = $aSettings['fc_server_host'];
	                $server_host = str_replace('http://','',$server_host);
	                $server_host = str_replace('/','',$server_host);
					$loc = 2;
					if ($aSettings['fc_client_loc'] == ''){
	                   $aSettings['fc_client_loc'] = 'http://' . $server_host . ':' . $aSettings['fc_http_port'] . '/';
	                   $c_own = Phpfox::getService('topcmmchat') -> validate_client('http://' . $server_host . ':' . $aSettings['fc_http_port'] . '/');
	                }else{
	                    $url = $aSettings['fc_client_loc'];
	                    $url = 'http://'.str_replace('http://','',$url);
	                    if(substr($url,-1,1) != "/"){
	                        $url .= '/';
	                    }
	                    $aSettings['fc_client_loc'] = $url;
	                    $c_own = Phpfox::getService('topcmmchat') -> validate_client($url);
	                }
	                $urlParse = parse_url($aSettings['fc_client_loc']);
	                if($urlParse['host'] != $aSettings['fc_server_host']){
	                    $loc = 2;
	                }
	                if ($c_own == 0){
	                    $loc = 0;
	                    $c_loc = 0;
	                }
	            }else{
					$aSettings['fc_server_host'] = $_SERVER['HTTP_HOST'];
					$aSettings['fc_server_port'] = '51127';
					$aSettings['fc_http_port'] = '35555';
	                $aSettings['fc_client_loc'] = $this->request()->get('fc_client_loc');
	                if ($aSettings['fc_client_loc'] == ''){
	                    $aSettings['fc_client_loc'] = 'http://' . $aSettings['fc_server_host'] . ':' . $aSettings['fc_http_port'] . '/';
	                    $c_own = Phpfox::getService('topcmmchat') -> validate_client('http://' . $aSettings['fc_server_host'] . ':' . $aSettings['fc_http_port'] . '/');
	                }else{
	                    $url = $aSettings['fc_client_loc'];
	                    $url = 'http://'.str_replace('http://','',$url);
						if(substr($url,-1,1) != "/"){
	                        $url .= '/';
	                    }
	                    $aSettings['fc_client_loc'] = $url;
	                    $c_own = Phpfox::getService('topcmmchat') -> validate_client($url);
	                    $clientparse = parse_url($url);
	                    $da = Phpfox::getService('topcmmchat') -> validate_client('http://' . $clientparse['host'] . ':' . $aSettings['fc_http_port'] . '/');
	                }
	                $urlParse = parse_url($aSettings['fc_client_loc']);
	                if($urlParse['host'] != $aSettings['fc_server_host']){
	                    $loc = 2;
	                }
	                $loc = 1;
	                if ($c_own == 0 && $da == 0){
	                    $loc = 0;
	                }
	            }
	            $aSettings['fc_api_url'] = 'http://'.$aSettings['fc_server_host'].':'.$aSettings['fc_http_port'].'/';
	        }elseif ($aSettings['fc_extendserver'] == '1'){
	            $aSettings['fc_client_loc'] = $this->request()->get('fc_client_loc');
	            $url = $aSettings['fc_client_loc'];
	            $url = 'http://'.str_replace('http://','',$url);
	            //$parseurl = parse_url($url);
	            $aSettings['fc_server_host'] = @parse_url($url, PHP_URL_HOST);
	            if ($content = topcmmRequestRemote::requestRemote($url,1)){
			        preg_match('/init_port=([0-9]*)/', $content, $matches);
			        $aSettings['fc_server_port'] = $matches[1];
			    }else{
			    	$aSettings['fc_server_port'] = "21127";
			    }
			    $aSettings['fc_api_url'] = 'http://' . $aSettings['fc_server_host'] . '/';				
				$aSettings['fc_group'] = substr(@parse_url($url, PHP_URL_PATH),1,-1);
	            if(substr($url,-1,1) != "/"){
	                $url .= '/';
	            }
	            $temp = $url;	  
	            if ($urlinfo = Phpfox::getService('topcmmchat')->getHostParameters($url)){
	                $loc = 0;
	            }else{
	                $loc = 1;
	            }
	            $c_own = Phpfox::getService('topcmmchat')->validate_client($url);
	            if($c_own == 0){
	                $loc = 0;
	            }else{
	                $loc = 1;
	            }
	            //echo $loc;
	            //exit;
	        }else{
				if ($this->request()->get('fc_room_name') != ''){
	                $aSettings['fc_room_name'] = $this->request()->get('fc_room_name');
	            }else{
	                $aSettings['fc_room_name'] = $_SERVER['HTTP_HOST'];
	            }
	            $loc = 0;
	        }
	        $aSettings['fc_room_list'] = $this->request()->get('fc_room_list');
			$aSettings['fc_user_list'] = $this->request()->get('fc_user_list');
			$aSettings['fc_fullscreen'] = $this->request()->get('fc_fullscreen');
			$aSettings['fc_client_lang'] = $this->request()->get('fc_client_lang');
			$aSettings['fc_client_skin'] = $this->request()->get('fc_client_skin');
	        if ($this->request()->get('fc_client_width') != ''){
				$aSettings['fc_client_width'] = $this->request()->get('fc_client_width');
	        }else{
				$aSettings['fc_client_width'] = '800';
			}
			if ($this->request()->get('fc_client_height') != ''){
	            $aSettings['fc_client_height'] = $this->request()->get('fc_client_height');
	        }else{
	            $aSettings['fc_client_height'] = '600';
	        }
	        if ($loc == 0){
				if (Phpfox::getService('topcmmchat.process')->updateSettings($aSettings)){
					$showmessenge = 'Settings edited successfully';
					//$this->url()->send('admincp.topcmmchat', null, 'Settings edited successfully');
				}else{
					$showmessenge = 'Settings edited failure ';
					//$this->url()->send('admincp.topcmmchat', null, 'Settings edited failure');
				}
			}elseif ($loc == 1){
				$showmessenge = 'Client Location is configured incorrectly';
				//$this->url()->send('admincp.topcmmchat', null, 'Client Location is configured incorrectly');
			}elseif ($loc == 2){
				$showmessenge = 'Chat Server host, port or http_port is configured incorrectly';
				//$this->url()->send('admincp.topcmmchat', null, 'Chat Server host, port or http_port is configured incorrectly');
			}
			if(isset($showmessenge)){
				$this->url()->send('admincp.topcmmchat', null, $showmessenge);
			}
        }
        $login_chat = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        $server_name = str_replace('index.php', 'module/topcmmchat/static/image/', $login_chat);
        $login_chat = str_replace('index.php','login_chat.php?username=%username%&password=%password%',$login_chat);
        $fc_download_url = 'http://www.123flashchat.com/download-now.html?p=123flashchat.' . ((PATH_SEPARATOR==':') ? (stristr(PHP_OS, 'darwin') ?  'dmg' : 'sh') : 'exe');
        $this->template()->assign(array(
                'settings' => Phpfox::getService('topcmmchat')->getSettings(),
                'login_chat' => $login_chat,
                'c_loc' => $c_loc,
                'download' => $fc_download_url,
                'server_name' => $server_name
                )
        );

    }
}

?>